package com.zzsy.shoppingpro.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.app.Config;
import com.zzsy.shoppingpro.basebean.BaseBean;
import com.zzsy.shoppingpro.commentactivity.GoodsInfoActivity;
import com.zzsy.shoppingpro.home.acitivty.HotDeticActivity;
import com.zzsy.shoppingpro.home.acitivty.SeckillDeticActivity;
import com.zzsy.shoppingpro.home.bean.GoodsBean;
import com.zzsy.shoppingpro.home.bean.HomeBean;
import com.zzsy.shoppingpro.home.acitivty.RecommendDeticActivity;
import com.zzsy.shoppingpro.home.bean.RecommendBean;
import com.zzsy.shoppingpro.home.utils.AlphaPageTransformer;
import com.zzsy.shoppingpro.home.utils.ScaleInTransformer;
import com.zzsy.shoppingpro.utils.Toast;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2019/8/19 0019.
 */

public class HomeRecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    /**
     * 上下文
     */
    private Context mContext;

    /**
     * 五种类型
     * */
    /**
     * 横幅广告
     */
    public static final int BANNER = 0;
    /**
     * 频道
     */
    public static final int CHANNEL = 1;
    /**
     * 活动
     */
    public static final int ACT = 2;
    /**
     * 秒杀
     */
    public static final int SECKILL = 3;
    /**
     * 推荐
     */
    public static final int RECOMMEND = 4;
    /**
     * 热卖
     */
    public static final int HOT = 5;

    /**
     * 当前类型
     */
    public int currentType = BANNER;

    /**
     * 数据对象
     */
    private BaseBean<HomeBean.ResultBean> resultBean;

    /**
     * 加载布局方法
     */
    private LayoutInflater mLayoutInflater;


    public HomeRecycleAdapter(Context context, BaseBean<HomeBean.ResultBean> resultBeanBaseBean) {
        this.mContext = context;
        this.resultBean = resultBeanBaseBean;
        mLayoutInflater = LayoutInflater.from(mContext);

    }


    /**
     * 根据位置得到类型—系统调用
     */

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case BANNER:
                currentType = BANNER;
                break;
            case CHANNEL:
                currentType = CHANNEL;
                break;
            case ACT:
                currentType = ACT;
                break;
            case SECKILL:
                currentType = SECKILL;
                break;
            case RECOMMEND:
                currentType = RECOMMEND;
                break;
            case HOT:
                currentType = HOT;
                break;
        }

        return currentType;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == BANNER) {
            return new BannerViewHolder(mLayoutInflater.inflate(R.layout.banner_viewpager, null), mContext, resultBean);
        } else if (viewType == CHANNEL) {
            return new ChannelViewHolder(mLayoutInflater.inflate(R.layout.channel_item, null), mContext);
        } else if (viewType == ACT) {
            return new ActViewHolder(mLayoutInflater.inflate(R.layout.act_item, null), mContext);
        } else if (viewType == SECKILL) {
            return new SeckillViewHolder(mLayoutInflater.inflate(R.layout.seckill_item, null), mContext);
        } else if (viewType == RECOMMEND) {
            return new RecommendViewHolder(mLayoutInflater.inflate(R.layout.recommend_item, null), mContext);
        }else if(viewType == HOT){
            return new HotViewHolder(mLayoutInflater.inflate(R.layout.hot_item,null),mContext);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == BANNER) {
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            bannerViewHolder.setData(resultBean.getData().getBanner_info());
        } else if (getItemViewType(position) == CHANNEL) {
            ChannelViewHolder channelViewHolder = (ChannelViewHolder) holder;
            channelViewHolder.setData(resultBean.getData().getChannel_info());
        } else if (getItemViewType(position) == ACT) {
            ActViewHolder actViewHolder = (ActViewHolder) holder;
            actViewHolder.setData(resultBean.getData().getAct_info());
        } else if (getItemViewType(position) == SECKILL) {
            SeckillViewHolder seckillViewHolder = (SeckillViewHolder) holder;
            seckillViewHolder.setData(resultBean.getData().getSeckill_info());
        } else if (getItemViewType(position) == RECOMMEND) {
            RecommendViewHolder recommendViewHolder = (RecommendViewHolder) holder;
            recommendViewHolder.setData(resultBean.getData().getRecommend_info());
        }else if(getItemViewType(position) == HOT){
            HotViewHolder viewHolder = (HotViewHolder) holder;
            viewHolder.setData(resultBean.getData().getHot_info());
        }
    }

    /**
     * 返回条目数 共6种
     */
    @Override
    public int getItemCount() {
        return 6;
    }


    /**
     * 轮播布局
     */
    class BannerViewHolder extends RecyclerView.ViewHolder {

        public Banner banner;
        public Context context;
        public BaseBean<HomeBean.ResultBean> resultBeanBaseBean;

        public BannerViewHolder(View itemView, Context mContext, BaseBean<HomeBean.ResultBean> resultBean) {
            super(itemView);
            banner = itemView.findViewById(R.id.banner);
            this.context = mContext;
            this.resultBeanBaseBean = resultBean;
            try {
                banner.setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        if (context != null && imageView != null) {

                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                            Glide.with(context)
                                    .load(Config.BASE_URl_IMAGE + path)
                                    .into(imageView);
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        /**
         * 添加banner轮播数据
         */
        public void setData(final List<HomeBean.ResultBean.BannerInfoBean> banner_info) {
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            //如果你想用自己项目的图片加载，自定义图片加载框架
            List<String> imageUrl = new ArrayList<>();
            for (int i = 0; i < banner_info.size(); i++) {
                imageUrl.add(banner_info.get(i).getImage());
            }
            try {
                if (imageUrl.size() > 0) {
                    banner.setImages(imageUrl);
                    banner.start();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            /**
             * 设置点击事件
             * */
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {
                    Toast.show(context, "点击了");
                }
            });


        }
    }

    /**
     * 频道
     */
    private class ChannelViewHolder extends RecyclerView.ViewHolder {
        public GridView gvChannel;
        public Context mContext;

        public ChannelViewHolder(View itemview, Context mContext) {
            super(itemview);
            gvChannel = itemview.findViewById(R.id.gv_channel);
            this.mContext = mContext;
        }


        public void setData(List<HomeBean.ResultBean.ChannelInfoBean> channel_info) {
            gvChannel.setAdapter(new ChannelAdapter(mContext, channel_info));
            //点击事件
            gvChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.show(mContext, "点击了" + position);
                }
            });

        }
    }

    /**
     * 活动的viewHolder
     */
    private class ActViewHolder extends RecyclerView.ViewHolder {

        public ViewPager actViewPager;
        public Context context;

        public ActViewHolder(View itemView, Context mContext) {
            super(itemView);
            actViewPager = itemView.findViewById(R.id.act_view_pager);
            this.context = mContext;

        }

        public void setData(List<HomeBean.ResultBean.ActInfoBean> act_info) {
            actViewPager.setPageMargin(20);
            actViewPager.setOffscreenPageLimit(3);
            actViewPager.setPageTransformer(true, new AlphaPageTransformer(new ScaleInTransformer()));
            actViewPager.setAdapter(new PagerAdapter() {
                @Override
                public int getCount() {
                    return act_info.size();
                }

                @Override
                public boolean isViewFromObject(View view, Object object) {

                    return view == object;
                }

                @Override
                public Object instantiateItem(ViewGroup container, int position) {
                    ImageView view = new ImageView(mContext);
                    view.setScaleType(ImageView.ScaleType.FIT_XY);
                    //绑定数据
                    Glide
                            .with(mContext)
                            .load(Config.BASE_URl_IMAGE + act_info.get(position).getIcon_url())
                            .into(view);
                    container.addView(view);
                    return view;
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
                    container.removeView((View) object);
                }
            });

            //点击事件
            actViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    Toast.show(mContext, "position:" + position);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
    }


    /**
     * 倒计时抢购viewHolder
     */
    private boolean isFirst = true;
    TextView tvTime;
    private int dt;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                dt = dt - 1000;
                SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss");
                tvTime.setText(sd.format(new Date(dt)));

                handler.removeMessages(0);
                handler.sendEmptyMessageDelayed(0, 1000);
                if (dt == 0) {
                    handler.removeMessages(0);
                }
            }
        }
    };

    private class SeckillViewHolder extends RecyclerView.ViewHolder {


        private final TextView tvMore;
        private final RecyclerView rvSeckill;
        public Context mContext;

        public SeckillViewHolder(View itemView, Context mContext) {
            super(itemView);
            this.mContext = mContext;
            tvTime = itemView.findViewById(R.id.tv_time);
            tvMore = itemView.findViewById(R.id.tv_more);
            rvSeckill = itemView.findViewById(R.id.rv_seckill);

        }

        public void setData(HomeBean.ResultBean.SeckillInfoBean seckill_info) {
            //点击更多数据
            tvMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext,SeckillDeticActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("seckill", (Serializable) seckill_info.getList());
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            });

            //设置时间
            if (isFirst) {
                dt = (int) (Integer.parseInt(seckill_info.getEnd_time()) - (Integer.parseInt(seckill_info.getStart_time())));
                isFirst = false;
            }

            //设置recyclerView的数据
            rvSeckill.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            SeckillRecyclerViewAdapter seckillRecyclerViewAdapter = new SeckillRecyclerViewAdapter(mContext, seckill_info);
            rvSeckill.setAdapter(seckillRecyclerViewAdapter);

            //倒计时
            handler.sendEmptyMessageDelayed(0, 1000);
            seckillRecyclerViewAdapter.setOnSeckillClickListener(new SeckillRecyclerViewAdapter.OnSeckillClickListener() {
                @Override
                public void onClick(int position) {
                    String cover_price = seckill_info.getList().get(position).getCover_price();
                    String name = seckill_info.getList().get(position).getName();
                    String figure = seckill_info.getList().get(position).getFigure();
                    String product_id = seckill_info.getList().get(position).getProduct_id();
                    GoodsBean goodsBean = new GoodsBean(name, cover_price, figure, product_id);

                    Intent intent = new Intent(mContext, GoodsInfoActivity.class);
                    intent.putExtra(Config.GOODS_BEAN, goodsBean);
                    mContext.startActivity(intent);
                }
            });
        }
    }


    /**
     * 推荐的viewHolder的展示
     */
    private class RecommendViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_more_recommend;
        private GridView gv_recommend;
        private Context mContext;

        public RecommendViewHolder(View itemView, Context mContext) {
            super(itemView);
            tv_more_recommend = itemView.findViewById(R.id.tv_more_recommend);
            gv_recommend = itemView.findViewById(R.id.gv_recommend);
            this.mContext = mContext;

        }

        public void setData(List<HomeBean.ResultBean.RecommendInfoBean> recommend_info) {
            RecommendGridViewAdapter recommendGridViewAdapter = new RecommendGridViewAdapter(mContext, recommend_info);
            gv_recommend.setAdapter(recommendGridViewAdapter);
            gv_recommend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String cover_price = recommend_info.get(position).getCover_price();
                    String name = recommend_info.get(position).getName();
                    String figure = recommend_info.get(position).getFigure();
                    String product_id = recommend_info.get(position).getProduct_id();
                    GoodsBean goodsBean = new GoodsBean(name, cover_price, figure, product_id);

                    Intent intent = new Intent(mContext, GoodsInfoActivity.class);
                    intent.putExtra(Config.GOODS_BEAN, goodsBean);
                    mContext.startActivity(intent);
                }
            });
            //查看更多添加点击事件
            tv_more_recommend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext,RecommendDeticActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("recommend", (Serializable) recommend_info);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            });
        }
    }


    /**
     * 热卖的viewHolder的展示
     * */
    private class HotViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_more_hot;
        private GridView gv_hot;
        private Context mContext;


        public HotViewHolder(View itemView, Context mContext) {
            super(itemView);
            tv_more_hot = itemView.findViewById(R.id.tv_more_hot);
            gv_hot = itemView.findViewById(R.id.gv_hot);
            this.mContext = mContext;
        }

        public void setData(List<HomeBean.ResultBean.HotInfoBean> hot_info) {
            tv_more_hot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext,HotDeticActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("hotbean", (Serializable) hot_info);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            });
            HotGridViewAdapter hotGridViewAdapter = new HotGridViewAdapter(mContext,hot_info);
            gv_hot.setAdapter(hotGridViewAdapter);
            gv_hot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String cover_price = hot_info.get(position).getCover_price();
                    String name = hot_info.get(position).getName();
                    String figure = hot_info.get(position).getFigure();
                    String product_id = hot_info.get(position).getProduct_id();
                    GoodsBean goodsBean = new GoodsBean(name, cover_price, figure, product_id);

                    Intent intent = new Intent(mContext, GoodsInfoActivity.class);
                    intent.putExtra(Config.GOODS_BEAN, goodsBean);
                    mContext.startActivity(intent);
                }
            });

        }
    }
}
